﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace JukeBoxLib
{

    public delegate void UpdateProgressDelegate(int progress);
    public delegate void EndScanDelegate();
    public class Scanner
    {
        public event UpdateProgressDelegate UpdateProgress;
        public event EndScanDelegate EndScan;

        private Tracks _tracks;
        private List<System.IO.FileInfo> _files;
        private Thread _workerThread;
        private bool _workerThreadStop;

        public int Progress { get; private set; }
        public int Count { get; private set; }

        public string Path { get; set; }
        public Tracks Tracks
        {
            get
            {
                return _tracks;
            }
        }

        public Scanner()
        {
            this.Path = "";
        }

        public Scanner(string path)
        {
            this.Path = path;
            Scan();
        }

        private int Scan()
        {
            this.Progress = 0;
            this.Count = 0;
            _tracks = new Tracks();
            _files = new List<System.IO.FileInfo>();

            Scan(this.Path, _files);
            
            this.Count = _files.Count;
            _workerThread = new Thread(this.ScanTask);
            _workerThreadStop = false;
            _workerThread.Start();
            return this.Count;
        }

        private void ScanTask()
        {
            foreach (System.IO.FileInfo file in _files)
            {
                if (_workerThreadStop)
                    break;
                _tracks.Add(file);
                this.Progress++;
                UpdateProgress(this.Progress);
            }
            _files = null;
            EndScan();
        }

        public bool IsScanning()
        {
            return _workerThread.IsAlive;
        }

        public void StopScanning()
        {
            if (_workerThread.IsAlive)
            {
                _workerThread.Abort();
                _workerThread.Join(10000);
            }
            _workerThreadStop = false;
        }

        private void Scan(string path, List<System.IO.FileInfo> files)
        {
            if (String.IsNullOrEmpty(path))
                return;

            if (!System.IO.Directory.Exists(path))
                return;

            if (_tracks == null || _files == null)
                return;

            System.IO.DirectoryInfo folder = new System.IO.DirectoryInfo(path);

            foreach (System.IO.FileInfo file in folder.GetFiles())
            {
                if (file.Extension.ToLower() == ".mp3" || file.Extension.ToLower() == ".wma")
                {
                    files.Add(file);
                }
            }

            foreach (System.IO.DirectoryInfo subfolder in folder.GetDirectories())
            {
                Scan(subfolder.FullName, files);
            }
        }
    }
}
